package com.example.bilibili_miaosha.utils;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Component;

@Component
public class MD5Util {
    public static String md5(String src){
        return DigestUtils.md2Hex(src);
    }
    private static final String salt ="1a2b3c4d";

    /**
     * 第一次MD5加密，生成表单传递的pass,在前端进行
     * @param inputPass
     * @return
     */
    public  static String inputPassToFromPass(String inputPass){
        String str = "" +salt.charAt(0)+salt.charAt(2)+inputPass+salt.charAt(5)+salt.charAt(4);
        return md5(str);
    }

    /**
     * 也是第一次MD5加密，生成存储在数据库中的pass,也在前端进行
     * @param fromPass
     * @param salt
     * @return
     */
    public  static String formPassToDBPass(String fromPass,String salt){
        String str = "" +salt.charAt(0)+salt.charAt(2)+fromPass+salt.charAt(5)+salt.charAt(4);
        return md5(str);
    }

    /**
     * 真正被调用的函数
     * @param inputPass
     * @param salt
     * @return
     */
    public static String inputPassToDBPass(String inputPass,String salt){
        String formPass = inputPassToFromPass(inputPass);
        String dbPass = formPassToDBPass(formPass,salt);
        return dbPass;
    }

//    main作为测试验证

    public static void main(String[] args) {
        //d3b1294a61a07da9b49b6e22b2cbd7f9(15316135973用户从前端传递回来的密码123456)
        //数据库中的密码：68156ba662e422b83001bdc017eb83bc
        System.out.println(inputPassToFromPass("123456"));
        System.out.println(formPassToDBPass("d3b1294a61a07da9b49b6e22b2cbd7f9","1a2b3c"));
        System.out.println(inputPassToDBPass("123456","1a2b3c4d"));


    }
}
